<?php

namespace Addons\OaSystem\Controller;
use Addons\OaSystem\Controller\OaDutyBaseController;
use Addons\Payment\PaymentAddon;

class OaTestController extends OaDutyBaseController
{
    protected $model;
    function _initialize()
    {
        $this->model = $this->getModel('oa_user_role');
        parent::_initialize();

    }

    private function buildMembershipTree(&$root,$dept){
        foreach ($root as $key => $value) {
            if($key == $dept['pid']){
                $map['dept_id'] =  $dept['id'];
                $members = M('OaEmployee')->where($map)->field('id,name')->select();
                foreach ($members as $member) {
                    $ur_map['emp_id'] = $member['id'];
                    $roleids = M('OaUserRole')->where($ur_map)->field('role_id')->select();
                    $roles = [];
                    foreach ($roleids as $roleid) {
                        $r_map['id'] = $roleid;
                        $role = M('OaRole')->where($r_map)->find();
                        array_push($roles,$role['name']);
                    }
                    $member['roles'] = $roles;
                }
                $root[$key]['subDept'][$dept['id']] = array(
                    'member'=>$members,
                    'name'=>$dept['name'],
                    'subDept'=>array());
                return true;
            }else{
                if(!empty($value['subDept'])){
                    if($this->buildMembershipTree($root[$key]['subDept'],$dept)){
                        return true;
                    }else{
                        continue;
                    }
                }
            }
        }
        return false;
    }
    private function buildTreeForHtml($root){
        $htmlStr = '';
        foreach ($root as $key => $value) {
            $htmlStr .= '<li><span class="folder" style="font-weight:bold;color:#002b36">' . $value['name'] . '</span>';
            $htmlStr .= '<ul>';
            foreach ($value['member'] as $item) {
                $htmlStr .= '<li class="closed"><span class="folder" style="color:#0fb1da;" >' . $item['name'] . '</span>';
                if(!empty($item['roles'])) {
                    $htmlStr .= '<ul>';
                    foreach ($item['roles'] as $role) {
                        $htmlStr .= '<li><span class="file" style="color:#00CC33;">' . $role . '</span></li>';
                    }
                    $htmlStr .= '</ul>';
                }
                $htmlStr .= '</li>';
            }
            if (!empty($value['subDept'])) {
                $htmlStr .= $this->buildTreeForHtml($value['subDept'],$htmlStr);
            }
            $htmlStr .= '</ul></li>';
        }
        return $htmlStr;
    }

    function lists(){
        $detps = D('OaDeptView')->order('level')->select();
        $data = array();
        foreach ($detps as $dept){
            if (empty($dept['pid'])){
                $map['dept_id'] =  $dept['id'];
                $members = M('OaEmployee')->where($map)->field('id,name')->select();
                for($i=0; $i< count($members); $i++) {
                    $ur_map['emp_id'] = $members[$i]['id'];
                    $roleids = M('OaUserRole')->where($ur_map)->field('emp_id,role_id')->select();
                    $roles = [];
                    foreach ($roleids as $roleid) {
                        $r_map['id'] = $roleid['role_id'];
                        $role = M('OaRole')->where($r_map)->find();
                        array_push($roles,$role['name']);
                    }
                    $members[$i]['roles'] = $roles;
                }

                $data[$dept['id']] = array(
                    'member'=>$members,
                    'name'=>$dept['name'],
                    'subDept'=>array());
            }else{
                $this->buildMembershipTree($data,$dept);
            }
        }
        $membersHtmlStr = $this->buildTreeForHtml($data);
        $this->assign('membershipTree',$membersHtmlStr);
        $this->display();
    }

    function add(){
        if(IS_POST) {
            $data['appid'] = $this->appId;
            $data['emp_id'] = $_POST['emp_id'];
            $role = M('oa_role')->select();
            foreach($role as $vo) {
                if($_POST[$vo['name']] == 1) {
                    $data['role_id'] = $vo['id'];
                    if(!db_find('oa_user_role', $data)) {
                        $data['assigned_by'] = 6;
                        M('oa_user_role')->add($data);
                    }
                }
            }
            $this->success('添加成功！', U ( 'lists'), 600);
        } else {
            $employee = db_select('oa_employee');
            $role = db_select('oa_role');
            $this->assign('employee', $employee);
            $this->assign('role', $role);
            $this->display('add');
        }
    }

    function del()
    {
        if(IS_POST) {
            //myprint($_POST);
            $del_ids = $_POST['ids'];
            foreach($del_ids as $del_duty_id) {
                $map['duty_id'] = $del_duty_id;
                M('oa_role_duty')->where($map)->delete();
            }
            $this->success('删除成功！', U ( 'lists'), 600);
        }

        //parent::del(); // TODO: Change the autogenerated stub
    }

    function print() {
        $name = M('OaEmployee')->where(array('id'=>'6'))->getField('name');
        var_dump($name);
        $this->display('print');
    }
    function refresh() {
        $applies = M('OaApplication')->select();
        foreach ($applies as $apply) {
            $apply['is_del'] = false;
            M('OaApplication')->where(array('id'=>$apply['id']))->save($apply);
        }
        var_dump('refresh');
    }

    function sendSupperiseMsg () {
        $data = M('OaNotice')->where(array('id'=>14))->find();
        D('OaEmpMsg')->sendSupperviseMsg(1,$data,true);
        // var_dump($data);
    }

    function sendNoticeMsg () {
        $data = M('OaNotice')->where(array('id'=>9))->find();
        $content['title'] = $data['title'];
        $content['time'] = date("Y/m/d h:i:s",$data['time']);
        $content['details'] = $data['details'];
        $content['from'] = $data['from_who'];
        D('OaEmpMsg')->sendNoticeMsg(1,$data['time'],$content,$data['id'],true);
        var_dump($data);
    }

    function sendAnnouceMsg () {
        $data = M('OaNotice')->where(array('id'=>32))->find();
        $content['title'] = $data['title'];
        $content['time'] = date("Y/m/d h:i:s",$data['time']);
        $content['details'] = $data['details'];
        $content['from'] = $data['from_who'];
        D('OaEmpMsg')->sendAnnoucementMsg(1,$data['time'],$content,$data['id'],true);
        // var_dump($data);
    }

    function setOderDept () {
        $items = M('OaServiceOrder')->select();
        foreach ($items as $item) {
            $item['deptid'] = 4;
            M('OaServiceOrder')->save($item);
        }
    }
}